/// <summary>
/// This class handles requests from the API and
/// sends them over IPC to the InterProcess class
/// </summary>

using namespace System;
using namespace System::Runtime::Remoting;
using namespace System::Runtime::Remoting::Channels;
using namespace System::Runtime::Remoting::Channels::Ipc;

#include "Interact.h"

#pragma managed

namespace API
{
	/// <summary>
	/// Constructs a new Interact object, connecting to the
	/// IPC server channel with the specified name.
	/// </summary>
	/// <param name="name">The name of the IPC server channel.</param>
	Interact::Interact(System::String ^ name)
	{
		try
		{
			// Retrieve the interprocess object from the C# application.
			this->m_InterProcess = (InterProcess ^) Activator::GetObject(
										InterProcess::typeid,
										"ipc://" + name + "/InterProcessManager");

			if (this->m_InterProcess == nullptr)
			{
				Console::WriteLine("Can't connect to RoketPack!");
				return;
			}
		}
		catch (RemotingException ^)
		{
			Console::WriteLine("Can't connect to RoketPack!");
			return;
		}
	}

	/// <summary>
	/// Frees up the resources used by Interact and disconnects
	/// from the server channel.
	/// </summary>
	Interact::~Interact()
	{
		this->m_InterProcess = nullptr;
	}

	bool Interact::RoketPackIsAvailable()
	{
		if (this->m_InterProcess == nullptr)
			return false;
		else
		{
			try
			{
				// Make a dummy request.
				this->m_InterProcess->ApplicationIsInstalled("https://pack.roket-enterprises.com/", "roketpack");

				// We don't care about the result of the request,
				// just whether we could make it without a
				// RemotingException being thrown.
				return true;
			}
			catch (RemotingException ^)
			{
				return false;
			}
		}
	}

	bool Interact::ApplicationIsInstalled(System::String ^ repository, System::String ^ application)
	{
		if (this->m_InterProcess == nullptr)
			return false;

		bool ret = this->m_InterProcess->ApplicationIsInstalled(repository, application);
		return ret;
	}

	bool Interact::ComponentIsInstalled(System::String ^ repository, System::String ^ application, System::String ^ component)
	{
		if (this->m_InterProcess == nullptr)
			return false;

		return this->m_InterProcess->ComponentIsInstalled(repository, application, component);
	}

	bool Interact::ComponentInstall(System::String ^ repository, System::String ^ application, System::String ^ component)
	{
		if (this->m_InterProcess == nullptr)
			return false;

		return this->m_InterProcess->ComponentInstall(repository, application, component);
	}

	bool Interact::ComponentUpdate(System::String ^ repository, System::String ^ application, System::String ^ component)
	{
		if (this->m_InterProcess == nullptr)
			return false;

		return this->m_InterProcess->ComponentUpdate(repository, application, component);
	}

	bool Interact::ComponentUninstall(System::String ^ repository, System::String ^ application, System::String ^ component)
	{
		if (this->m_InterProcess == nullptr)
			return false;

		return this->m_InterProcess->ComponentUninstall(repository, application, component);
	}

	bool Interact::VersionIsLatest(System::String ^ repository, System::String ^ application, System::String ^ component)
	{
		if (this->m_InterProcess == nullptr)
			return false;

		return this->m_InterProcess->VersionIsLatest(repository, application, component);
	}

	bool Interact::MiscellaneousCallURL(System::String ^ url)
	{
		if (this->m_InterProcess == nullptr)
			return false;

		return this->m_InterProcess->MiscellaneousCallURL(url);
	}

	bool Interact::AuthorizedCallURL(System::String ^ url)
	{
		if (this->m_InterProcess == nullptr)
			return false;

		return this->m_InterProcess->AuthorizedCallURL(url);
	}
}